export function renderMixin(Vue) {
    //标签
    Vue.prototype._c = function () {
        return createElement(this, ...arguments)
    }
    //文本
    Vue.prototype._v = function (text) {
        return createText(text)
    }
    //变量
    Vue.prototype._s = function (val) {
        return val = null ? '' : typeof val === 'object' ? JSON.stringify(val) : val
    }
    // 渲染虚拟DOM
    Vue.prototype._render = function () {
        let vm = this
        let render = vm.$options.render
        let vnode = render.call(this)
        return vnode
    }
}

function createElement(vm, tag, data = {}, ...children) {
    if (isResved(tag)) {
        return vnode(vm, tag, data, data.key, children)
    } else {
        const Ctor = vm.$options['components'][tag]

        return createComponent(vm, tag, data, children, Ctor)
    }
}
function createComponent(vm, tag, data, children, Ctor) {
    if (typeof Ctor === 'object') {
        Ctor = vm.constructor.extend(Ctor)
    }
    data.hook = {
        init() {
            let child = vnode.componentInstance = new vnode.componentOptions.Ctor({})
            child.$mount()
        }
    }
    return vnode('vm', 'vue-component' + '-' + tag, data, undefined, undefined, undefined, { Ctor, children })
}

function isResved(tag) {
    return ['a', 'div', 'p'].includes(tag)
}

function createText(text) {
    return vnode(undefined, undefined, undefined, undefined, text)
}

function vnode(vm, tag, data, key, children, text, componentOptions) {
    return {
        vm, tag, data, key, children, text, componentOptions
    }
}